package com.athuawei.nsync;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 同步：
 * 异步：Comp
 * 异步调用回调
 */
public class NSyncCaller {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        // 1.异步调用 没有返回值
        CompletableFuture<Void> completableFuture1 = CompletableFuture.runAsync(
                ()->{
                    System.out.println(Thread.currentThread().getName()
                            + " completableFuture1");
                }
        );
        completableFuture1.get();

        // 2.异步调用 有返回值
        CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(
                ()->{
                    System.out.println(Thread.currentThread().getName()
                    + "completableFuture2");
                    return 1024;
                });
        completableFuture2.whenComplete((t,u)->{
            System.out.println("--------t = " + t);//方法的返回值
            System.out.println("--------u = " + u);//异常
        }).get();
    }
}
